package com.atguigu.dga.meta.service.impl;

import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.constant.CodeConst;
import com.atguigu.dga.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.atguigu.dga.meta.service.TableMetaInfoService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2023-11-20
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {

    @Autowired
    TableMetaInfoService tableMetaInfoService;

    public  void initTableMetaInfoExtra(List<TableMetaInfo> tableMetaInfoNonExistsExtraList ) {
        // 1  要查询哪些表需要初始化辅助信息  只有没有辅助信息表的表 才需要初始化辅助信息
       //select  * from  table_meta_info
        //    where  assess_date ='' and
        //          concat(table_name,',',schema_name)  not in (select concat(table_name,',',schema_name)  from table_meta_info_extra)

        tableMetaInfoService.list();
        List<TableMetaInfoExtra> tableMetaInfoExtraList=new ArrayList<>(tableMetaInfoNonExistsExtraList.size());
        for (TableMetaInfo tableMetaInfo : tableMetaInfoNonExistsExtraList) {
            TableMetaInfoExtra tableMetaInfoExtra = new TableMetaInfoExtra();

            tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
            tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());
            //负责人
            tableMetaInfoExtra.setTecOwnerUserName("未填写");
            tableMetaInfoExtra.setBusiOwnerUserName("未填写");
            // 分层
            tableMetaInfoExtra.setDwLevel( getDwLevelByName(tableMetaInfo.getTableName()));
            //安全
            tableMetaInfoExtra.setSecurityLevel(CodeConst.SECURITY_LEVEL_UNSET);
            //生命周期
            tableMetaInfoExtra.setLifecycleType(CodeConst.LIFECYCLE_TYPE_UNSET);
            tableMetaInfoExtra.setLifecycleDays(-1L);

            tableMetaInfoExtra.setCreateTime(new Date());
            tableMetaInfoExtraList.add(tableMetaInfoExtra);
        }

        saveOrUpdateBatch(tableMetaInfoExtraList );
    }

    private String getDwLevelByName(String tableName) {
        if(tableName.startsWith("ods")){
            return "ODS";
        } else if (tableName.startsWith("dwd")) {
            return "DWD";
        }else if (tableName.startsWith("dim")) {
            return "DIM";
        }else if (tableName.startsWith("dws")) {
            return "DWS";
        }else if (tableName.startsWith("ads")) {
            return "ADS";
        }else if (tableName.startsWith("dm")) {
            return "DM";
        }else  {
            return "OTHER";
        }

    }
}
